require(['bbcare'], function ($bb) {

    var URL = $bb.baseUrl("followup")

        // URL = 'http://192.168.3.138:8090'

    var vm = new Vue({
        el: '#root',
        data: {
            search: {
                name: '',
                phone: '',
                status: '',
                date: '',
                start: null,
                end: null,
                loading: false
            },
            table: {
                data: [],
                total: 0,
                current: 1,
                pageSize: 10,
                loading: false
            },
        },
        created: function() {
            this.getTableData()
        },
        watch: {
            'search.date': function(val) {
                this.search.start = this.formatTime(val[0])
                this.search.end = this.formatTime(val[1])
            }
        },
        methods: {
            handleReset: function () {
                this.search.name = ''
                this.search.phone = ''
                this.search.status = ''
                this.search.date = null
                this.search.start = null
                this.search.end = null
                this.getTableData()
            },
            /**
             * 改变当前得页码
             * @param {*} current 
             */
            handleChangePage: function(current) {
                this.table.current = current
                this.getTableData()
            },
            /**
             * 判断当前预约状态
             */
            judgeStatus: function (type) {
                type = String(type)
                switch(type) {
                    case '1': 
                    return '进行中'
                    case '2':
                    return '已完成'
                    case '3': 
                    return '已过期'
                    case '4': 
                    return '待确认'
                }
            },
            /**
             * 点击查询
             */
            handleSearch: function() {
                this.search.loading = true
                this.getTableData()
            },
            /**
             * 获取列表数据
             */
            getTableData: function () {
                var that = this
                this.table.loading = true
                $bb.ajax({
                    url: URL + 'followup/appointment/record/listPage',
                    dataType: 'json',
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify({
                        currentPage: this.table.current,
                        pageSize: this.table.pageSize,
                        userName: this.search.name,
                        phone: this.search.phone,
                        status: this.search.status,
                        appointTimeStart: this.search.start,
                        appointTimeEnd: this.search.end
                    }),
                    success: function (res) {
                        if (!res.success) that.$message.error('请求错误，请重新尝试!!!')
                        res.data.data.forEach(function (item, index) {
                            item.No = (that.table.current - 1) * 10 + index + 1
                        })
                        that.table.data = res.data.data
                        that.table.total = res.data.count
                        that.search.loading = false
                        that.table.loading = false
                        that.toTop()
                    }
                })
            },
            /**
             * 日期格式化
             * @param { Date } date 
             * @returns { string } date
             */
            formatTime: function(date) {
                var year = date.getFullYear(),
                   month = date.getMonth() + 1,
                     day = date.getDate(),
                    hour = date.getHours(),
                     min = date.getMinutes(),
                     sec = date.getSeconds()

                    hour = hour == 0 ? '23' : hour
                     min = min  == 0 ? '59' : min
                     sec = sec  == 0 ? '59' : sec
                return year + '-' + month + '-' + day + ' ' + this.addZore(hour) + ':' + this.addZore(min) + ':' + this.addZore(sec)
            },
            /**
             * 小于10 则添加 0
             * @returns { string } 
             */
            addZore: function (num) {
                return num < 10 ? '0' + num : num
            },
            toTop: function () {
                var dom = document.getElementById('content-main')
                dom.scrollTo(0, 0)
            }
        }
    })

})

